<template>
    <div class="register_container">
        <div class="register_box">
            <h2 align='center'>注册系统</h2>
            <!-- 表单区域 -->
            <el-form ref="registerFormRef" :rules="registerRules" :model="registerForm" class="login_form" label-width="0">
                <!-- 用户名 -->
                <el-form-item prop="username">
                    <el-input type="text" placeholder="用户名" v-model="registerForm.username" prefix-icon="iconfont icon-denglu"></el-input>
                </el-form-item>
                <!-- 密码 -->
                <el-form-item prop="password">
                    <el-input type="password" placeholder="密码" v-model="registerForm.password" prefix-icon="iconfont icon-mima"></el-input>
                </el-form-item>
                <!-- 按钮 -->
                <el-form-item >
                    <el-button class="btl" type="primary" @click="RegisterSuccess" size="medium">确定注册</el-button>    
                </el-form-item>
                <el-form-item >
                    <el-button class="btl" type="text" @click="back" size="medium">已有账号，立即登录</el-button>    
                </el-form-item>
            </el-form>    
        </div>
    </div>
</template>
<script>
export default {
    data(){
        return{
            registerForm:{
                username:"",
                password:""
            },
            registerRules:{
                username:[
                    { required: true, message: "请输入用户名", trigger: "blur" },
                    { min: 2, max: 10, message: "用户名长度在 2 到 10 个字符", trigger: "blur" }
                ],
                password:[
                    { required: true, message: "请输入密码", trigger: "blur" },
                    { min: 6, max: 12, message: "密码长度为 6~12 位", trigger: "blur" }
                ],
            },
        };
    },
    methods:{
        RegisterSuccess(){
            this.$refs.registerFormRef.validate(async valid=>{
                if(!valid){
                    return;//验证失败
                }
                const {data:res}=await this.$http.post("register",this.registerForm);//访问后台
                if (res == "success" ) {
                    window.sessionStorage.setItem('flag','success'); // session 放置
                    this.$message.success("注册成功，请登录！！！");
                    this.$router.push({ path: "/login"});
                }else{
                    this.$message.error("注册失败，请重新注册！！！");
                }
            })
        },
        back(){
            this.$router.push({ path: "/login"});
        },
    },
    
}
</script>

<style lang="less" scoped>
    .register_container{
        background:url(https://z3.ax1x.com/2021/05/24/gje16x.jpg);
        background-color: rgba(9, 164, 216, 0.849);
        position:fixed;
        background-size:100% 100%;
        height:100%;
        width: 100%;
    }
    .register_box{
        background-color:rgba(255, 255,255, 0.4);
        width: 300px;
        height: 300px;
        border-radius: 3px; 
        padding: 20px ;      
        position: absolute;
        left: 50%;
        top:50%;
        transform: translate(-50%,-50%);
    }
    .btl{
        width: 100%;
        font-weight: bold;
    }
</style>
